<template>
	<view class="container">
		<view class="list">
			<view class="title">
				请输入原密码:
			</view>
			<view class="input">
				<input type="password" v-model="revisePassword.oldPass" placeholder="请输入原密码" placeholder-class="inputpl"/>
			</view>
		</view>
		<view class="list">
			<view class="title">
				请输入新密码:
			</view>
			<view class="input">
				<input type="password" v-model="revisePassword.newPass" placeholder="请输入新密码" placeholder-class="inputpl"/>
			</view>
		</view>
		<view class="list">
			<view class="title">
				请再次确认密码:
			</view>
			<view class="input">
				<input type="password" v-model="revisePassword.reqPass" placeholder="请再次确认密码" placeholder-class="inputpl"/>
			</view>
		</view>
		<view class="btn">
			<button type="default" @click="revisePass">确认修改</button>
		</view>
	</view>
</template>

<script>
	import api from '@/comon/api.js'
	export default {
		data(){
			return {
				userInfos:{
					
				},
				revisePassword:{
					oldPass: '', //旧密码
					newPass: '', //新密码
					reqPass: '' //确认新密码
				}
			}
		},
		onLoad() {
			let userinfo = uni.getStorageSync('userInfos');
			if(!userinfo){
				uni.navigateBack({})
			}else {
				this.userInfos = userinfo;
			}
		},
		methods:{
			async revisePass(){
				console.log(this.revisePassword)
				if(this.revisePassword.oldPass.length < 6){
					this.showToast('原密码长度不准确')
					return
				}
				if(this.revisePassword.newPass.length < 6){
					this.showToast('新密码长度不准确')
					return
				}
				if(this.revisePassword.newPass != this.revisePassword.reqPass){
					this.showToast('两次密码输入不一致')
					return
				}
				if(btoa(this.revisePassword.oldPass) != this.userInfos.loginPassWord){
					this.showToast('原密码输入错误')
					return
				}
				let res = await api.uniCloudApi(api.publicFun.user,{
					_id:this.userInfos._id,
					loginPassWord: btoa(this.revisePassword.newPass),
					user_type: 4
					});
				if(res){
					this.showToast('密码修改成功，请重新登录');
					uni.clearStorage();
					setTimeout(()=>{
						uni.redirectTo({
							url:'../public/login'
						})
					},1500)
				}
			},
			showToast(text){
				uni.showToast({
					icon:'none',
					title:text
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F1F1F1;
	}
	.list{
		background-color: #FFFFFF;
		width: 95%;
		padding: 2.5%;
		display: flex;
		height: 60rpx;
		.title{
			width: 35%;
			height: 60rpx;
			line-height: 60rpx;
		}
		.input{
			width: 65%;
			height: 60rpx;
			input{
				height: 50rpx;
				margin-top: 10rpx;
				border-bottom: 1px solid #F1F1F1;
			}
			.inputpl{
				font-size: 28rpx;
			}
		}
	}
	.btn{
		width: 70%;
		height: 80rpx;
		position: absolute;
		bottom: 100rpx;
		left: 15%;
		button{
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 40rpx;
			border: 0;
			background-color: $uni-back-color;
			color: #FFFFFF;
		}
	}
</style>
